<template>
  <div class="auther_table_wrap">
    <el-table
      :data="channels"
      border
      style="width: 95.2%; margin: 0px auto"
      size="medium"
    >
      <el-table-column prop="id" label="id" width="100"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名"> </el-table-column>
      <el-table-column prop="cate" label="一级分类id"> </el-table-column>
      <el-table-column prop="name" label="组名"> </el-table-column>
      <el-table-column prop="id" label="组id"> </el-table-column>
      <el-table-column prop="sequence" label="展示顺序"> </el-table-column>
      <el-table-column prop="group_url" label="频道地址" width="160">
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" @click="fnPopShow(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="fnDelChannel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改频道" :visible.sync="pop_show" append-to-body>
      <el-form
        :model="ChannelsForm"
        status-icon
        :rules="rulesChannelsForm"
        ref="ChannelsForm"
        label-width="100px"
      >
        <el-form-item label="频道组：" prop="group_name">
          <el-input v-model="ChannelsForm.group_name">
          </el-input>
          <!-- <el-select v-model="ChannelsForm.group_name" size="small">
            <el-option
              v-for="item in group_type_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="一级分类：" prop="cate">
          <el-select v-model="ChannelsForm.cate" size="small">
            <el-option
              v-for="item in category_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="频道地址：" prop="url">
          <el-input
            type="text"
            v-model="ChannelsForm.url"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="展示顺序：" prop="sequence">
          <el-input
            type="text"
            v-model="ChannelsForm.sequence"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm('ChannelsForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import cons from "@/components/constant";
let token = localStorage.token;
export default {
  name: "OptionsTable",
  props: ["channels"],
  data() {
    return {
      pop_show: false,
      group_type_list: [],
      category_list: [],   //所有的一级分类列表
      ChannelsForm: {
        cate: "",    // 一级类别id
        group_id: "",
        // category_id: "",
        url: "",
        group_name: "",
        sequence: "",
        cate_name: "",
      },
      rulesChannelsForm: {},
    };
  },
  methods: {
    // 点击编辑时触发的方法
    fnPopShow(id) {
      // this.pop_show 默认时false
      this.pop_show = true;
      this.edit_id = id;
      this.axios
        .get(cons.apis + "/goods/channels/" + this.edit_id + "/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          console.log("编辑一个频道组的响应", dat);
          this.ChannelsForm.group_name = dat.data.name;
          this.ChannelsForm.group_id = dat.data.id;
          this.ChannelsForm.url = dat.data.group_url;
          this.ChannelsForm.sequence = dat.data.sequence;
          this.ChannelsForm.cate_name = dat.data.cate_name;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    // 提交
    submitForm() {
      this.axios
        .put(
          cons.apis + "/goods/channels/" + this.edit_id + "/",
          {
            name: this.ChannelsForm.group_name,
            cate: this.ChannelsForm.cate,
            group_url: this.ChannelsForm.url,
            sequence: this.ChannelsForm.sequence,
          },
          {
            headers: {
              Authorization: "JWT " + token,
            },
            responseType: "json",
          }
        )
        .then((dat) => {
          this.$message({
            type: "success",
            message: "频道修改成功!",
          });
          this.pop_show = false;
          this.resetForm("ChannelsForm");
          this.$emit("fnResetTable");
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    // 删除
    fnDelChannel(id) {
      this.edit_id = id;
      this.$confirm("此操作将删除该频道, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .delete(cons.apis + "/goods/channels/" + this.edit_id + "/", {
              headers: {
                Authorization: "JWT " + token,
              },
              responseType: "json",
            })
            .then((dat) => {
              this.$message({
                type: "success",
                message: "删除频道成功!",
              });
              this.$emit("fnResetTable");
            })
            .catch((err) => {
              if (err.response.status == 404) {
                this.$message({
                  type: "info",
                  message: "频道未找到！",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取频道组的类型
    fnGetChannelType() {
      this.axios
        .get(cons.apis + "/channels/groups/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          this.group_type_list = dat.data.results;  // .results
        })
        .catch((err) => {
          console.log(err.response);
        });
    },

    // 获取所有的一级类别
    fnGetCategories() {
      this.axios
        .get(cons.apis + "/goods/channels/firstCate/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          console.log("加载所有的一级分类>>>", dat);
          this.category_list = dat.data;
          
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    // 获取类被组   // 获取频道组的类型
    this.fnGetChannelType();
    // 获取所有的一级类别信息
    this.fnGetCategories();
  },
};
</script>

<style scope>
.el-table th,
.el-table td {
  text-align: center;
}
</style>
